$(function () {
    function render() {
        $.ajax({
            url: "/o2o-merchant/sec/rest/merchant",
            type: "get"
        }).done(function (msg) {
            $("#userPortrait").attr("src", msg.portrait);
            $("#merchantInfo [name='username']").text(msg.username);
            $("#merchantInfo [name='address']").text(msg.address);
            $("#merchantInfo [name='portrait']").attr("src", msg.portrait);
            $("#merchantInfo [name='idCard']").attr("src", msg.idCard);
            $("#updateForm [name='address']").val(msg.address);
        });
        $.ajax({
            url: "/o2o-merchant/sec/rest/auditInfo",
            type: "get"
        }).done(function (msg) {
            console.log(msg);
            switch (msg.status) {
                case 0:
                    $("#merchantInfo [name='status']").text("待审核");
                    $("#statement").text("亲，您的信息还在审核中，请耐心等待！");
                    break;
                case 1:
                    $("#merchantInfo [name='status']").text("审核通过");
                    $("#statement").text("亲，您已通过审核，祝您生意兴隆，财源滚滚！");
                    $("#merchantInfo").find("button").remove();
                    break;
                case 2:

                    $("#merchantInfo [name='status']").text("被拉黑");
                    $("#statement").text("您已被拉黑，若有冤屈，可拨打号码0454-07777进行申诉！");
                    $("#merchantInfo").find("button").remove();
                    break;
                case 3:
                    $("#merchantInfo [name='status']").text("被驳回");
                    $("#statement").text("亲，您的信息还在被驳回，请再次提交！");
                    $("#merchantInfo [name='content']").text(msg.content);
                    break;
            }
            // $("#merchantInfo [name='status']").text(msg.status);
            // $("#merchantInfo [name='content']").text(msg.content);
        });
    }
    render();

    $("#submitUpdateBtn").on('click', function () {
        var formData = new FormData($("#updateForm").get(0));
        // validate whether field is empty
        var validate = true;
        $("#updateForm").find("input").each(function () {
            if (!$(this).val()) {
                var prompt = $(this);
                prompt.popover('show');
                setTimeout(function () {
                    prompt.popover('hide');
                }, 3000);
                validate = false;
            }
        });
        // pass
        if (validate) {
            var formData = new FormData($("#updateForm").get(0));
            for (let pair of formData.entries()) {
                console.log(pair[0] + pair[1]);
            }
            $.ajax({
                url: "/o2o-merchant/sec/rest/merchant/update.action",
                type: "post",
                data: formData,
                contentType: false,
                processData: false
            }).done(function (msg) {
                if (msg) {
                    alert("提交成功");
                    window.location.href = "/o2o-merchant/sec/merchant";
                    $("#updateModel").modal("hide");
                } else {
                    alert("重新审核失败");
                }
            });
        }
    });
    var ws = new WebSocket("ws://10.222.29.156:8081/o2o-merchant/ws/audit");
    ws.onmessage = function (event) {
        alert("信息发生改变");
        console.log(event.data);
        var msg = JSON.parse(event.data);
        switch (msg.status) {
            case 0:
                $("#merchantInfo [name='status']").text("待审核");
                $("#merchantInfo [name='content']").text("");
                break;
            case 1:
                $("#merchantInfo [name='status']").text("审核通过");
                $("#merchantInfo [name='content']").text("");
                $("#merchantInfo").find("button").remove();
                break;
            case 2:
                $("#merchantInfo [name='status']").text("被拉黑");
                $("#merchantInfo").find("button").remove();
                break;
            case 3:
                $("#merchantInfo [name='status']").text("被驳回");
                $("#merchantInfo [name='content']").text(msg.content);
                break;
        }
    }
});